 //
//  CommunityCell.m
//  Four_Fit
//
//  Created by lanou on 16/7/13.
//  Copyright © 2016年 xmaz. All rights reserved.
//

#import "CommunityCell.h"
#import "Community.h"
#import "comment.h"
#import "OnePicture.h"
#import "TowPicture.h"
#import "ThreePicture.h"

@interface CommunityCell ()
@property (weak, nonatomic) IBOutlet UIImageView *userpic;
@property (weak, nonatomic) IBOutlet UILabel *username;
@property (weak, nonatomic) IBOutlet UILabel *title;

@property (weak, nonatomic) IBOutlet UILabel *content;

@property (weak, nonatomic) IBOutlet UILabel *detailedContent;

@property (weak, nonatomic) IBOutlet UILabel *firstName;
@property (weak, nonatomic) IBOutlet UILabel *fristContent;
@property (weak, nonatomic) IBOutlet UILabel *secondName;
@property (weak, nonatomic) IBOutlet UILabel *secondContent;
@property (weak, nonatomic) IBOutlet UIView *commentView;

/** <#注释#> */
@property (nonatomic, strong) NSMutableArray *dataArray;
/** <#注释#> */
@property (nonatomic, strong) OnePicture *onePic;
/** <#注释#> */
@property (nonatomic, strong) TowPicture *towPic;
/** <#注释#> */
@property (nonatomic, strong) ThreePicture *threePic;

@end

@implementation CommunityCell

+ (instancetype)cell {
    
    return [[[NSBundle mainBundle] loadNibNamed:NSStringFromClass(self) owner:nil options:nil] firstObject];
}

- (OnePicture *)onePic
{
    if (!_onePic) {
        _onePic = [OnePicture onePicture];
        [self.contentView addSubview:_onePic];
        
    }
    return _onePic;
}

- (TowPicture *)towPic
{
    if (!_towPic) {
        _towPic = [TowPicture towPicture];
        [self.contentView addSubview:_towPic];
        
    }
    return _towPic;
}

- (ThreePicture *)threePic {
    if (!_threePic) {
        _threePic = [ThreePicture threePicture];
        [self.contentView addSubview:_threePic];
    }
    return _threePic;
}

- (void)awakeFromNib {
    
    self.userpic.layer.cornerRadius = self.userpic.frame.size.width * 0.5;
    self.userpic.layer.masksToBounds = YES;
    
}


- (NSMutableArray *)dataArray {
    if (!_dataArray) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

- (void)setCommunity:(Community *)community {
    [self.dataArray removeAllObjects];
    
    _community = community;
    
    [self.userpic sd_setImageWithURL:[NSURL URLWithString:community.userpic]];
    self.username.text = community.username;
    self.title.text = community.title;
    
    if (community.type == 0) {
        
        self.detailedContent.hidden = YES;
        self.content.hidden = NO;
        self.content.text = community.content;
        
    } else if(community.type == 1){
        
        self.content.hidden = YES;
        self.detailedContent.hidden = NO;
        // 文字的最大尺寸
        CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, MAXFLOAT);
        
        //计算文字所需要的高度
        CGFloat textH = [community.content boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
        [self.content layoutIfNeeded];
        self.content.height = textH + 10;

        self.detailedContent.text = community.content;
    }
   
    
    //设置评论
    for (NSDictionary *dic in community.items) {
        
        comment *cot = [[comment alloc] init];
        [cot setValuesForKeysWithDictionary:dic];
        [self.dataArray addObject:cot];
        
    }
    if (self.dataArray.count == 2 || self.dataArray.count == 3) {
        
        self.firstName.text = [self.dataArray[0] sendernickname];
        self.fristContent.text = [self.dataArray[0] content];
        self.secondName.text = [self.dataArray[1] sendernickname];
        self.secondContent.text = [self.dataArray[1] content];
       
        
    } else if (self.dataArray.count == 1) {
        
        self.firstName.text = [self.dataArray[0] sendernickname];
        self.fristContent.text = [self.dataArray[0] content];
        
        
    } else if (self.dataArray.count == 0){
        
        self.commentView.hidden = YES;
        
    }
    
    //设置图片
    if (community.pic.count == 1) {
        
        self.onePic.hidden = NO;
        self.onePic.community = community;
        self.onePic.frame = community.pictureViewFrame;
        self.towPic.hidden = YES;
        self.threePic.hidden = YES;
        
    } else if (community.pic.count == 2) {
        
        self.towPic.hidden = NO;
        self.towPic.community = community;
        self.towPic.frame = community.pictureViewFrame;

        self.onePic.hidden = YES;
        self.threePic.hidden = YES;
        
    } else if (community.pic.count == 3) {
        
        self.threePic.hidden = NO;
        self.threePic.community = community;
        self.threePic.frame = community.pictureViewFrame;
        self.onePic.hidden = YES;
        self.towPic.hidden = YES;
    }

}

- (void)setComment:(comment *)comment {
    
    _comment = comment;
    
    self.commentView.hidden = YES;
    
    [self.userpic sd_setImageWithURL:[NSURL URLWithString:comment.userpic]];
    self.username.text = comment.username;
    self.title.text = comment.title;
    
        
        self.content.hidden = YES;
        self.detailedContent.hidden = NO;
        // 文字的最大尺寸
        CGSize maxSize = CGSizeMake([UIScreen mainScreen].bounds.size.width - 40, MAXFLOAT);
        
        //计算文字所需要的高度
        CGFloat textH = [comment.content boundingRectWithSize:maxSize options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName : [UIFont systemFontOfSize:14]} context:nil].size.height;
    
        self.content.height = textH + 10;
        
        self.detailedContent.text = comment.content;
    
    
    //设置图片
    if (comment.pic.count == 1) {

        self.onePic.comment = comment;
        self.onePic.frame = comment.pictureViewFrame;
        
    } else if (comment.pic.count == 2) {
        
      
        self.towPic.comment = comment;
        self.towPic.frame = comment.pictureViewFrame;
        
        
    } else if (comment.pic.count == 3) {
                
        self.threePic.comment = comment;
        self.threePic.frame = comment.pictureViewFrame;
        
    }
    
    
}

    
    


@end
